package com.itmuch.cloud.microservicesimpleprovideruser.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.itmuch.cloud.microservicesimpleprovideruser.pojo.User;
import com.itmuch.cloud.microservicesimpleprovideruser.repository.UserRepository;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;


@RestController
public class UserController {

	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private EurekaClient discoveryClient;
	
	@Autowired
	private DiscoveryClient coveryClient;
	
	@GetMapping("/simple/{id}")
	public User findById(@PathVariable Long id){
		return userRepository.findOne(id);
	}
	
	@GetMapping("/instance-instance")
	public String serviceUrl(){		
		InstanceInfo instanceInfo = discoveryClient.getNextServerFromEureka("microservice-provider-user", false);
		return instanceInfo.getHomePageUrl();
	}
	
	@GetMapping("/instance-info")
	public ServiceInstance showInfo(){		
		ServiceInstance localServiceInstance = coveryClient.getLocalServiceInstance();
		return localServiceInstance;
	}
}
